package com.ship.dispatch.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ship.dispatch.bean.SpOverhaul;
import com.ship.dispatch.bean.SpQualifications;
import com.ship.dispatch.bo.SpQualificationsBo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public interface SpQualificationsMapper extends BaseMapper<SpQualifications> {

    /**
     * 根据船舶名字查询imo
     * @param nameOfVessel
     * @return
     */
    @Select("SELECT imo FROM sp_information WHERE name_of_vessel = #{nameOfVessel}")
    String getImoByName(String nameOfVessel);

    /**
     * 查询列表展示信息
     * @return
     */
    @Select("SELECT id,name_of_vessel,imo,cert_name,sign_date,sign_agency,expiration_date,expiration_time,remarks,create_date,create_by,del_apply FROM sp_qualifications WHERE del_apply = 0  ORDER BY create_date DESC")
    List<SpQualifications> getInfo();

    /**
     * 获取所有船只
     * @return
     */
    @Select("SELECT name_of_vessel FROM sp_information")
    List<String> getShipList();

    /**
     * 根据id,动态查询信息
     * @param ids
     * @return
     */
    @Select({
            "<script>",
            "SELECT id,name_of_vessel,imo,cert_name,sign_date,sign_agency,expiration_date,expiration_time,remarks,create_date,create_by ",
            "FROM sp_qualifications ",
            "WHERE del_apply = 0 AND id IN ",
            "<foreach item='item' index='index' collection='arg0' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    List<SpQualifications> getInFOById(List<String> ids);

    /**
     * 根据id获取文件下载路径
     * @param id
     * @return
     */
    @Select("SELECT path FROM sp_fob_file_record WHERE overhaul_id = #{id}")
    List<String> getFileById(Long id);

    /**
     * 根据id查询信息
     * @param id
     * @return
     */
    @Select("SELECT id, name_of_vessel, imo, cert_name,sign_date,sign_agency,expiration_date,expiration_time,remarks FROM sp_qualifications WHERE del_apply = 0 AND id = #{id} ")
    SpQualifications getById(String id);

    /**
     * 根据id返回文件下载路径及其文件id
     * @param id
     * @return
     */
    @Select("SELECT path AS url, id AS fileId FROM sp_fob_file_record WHERE overhaul_id = #{id}")
    List<Map<String, String>> getFileIdById(Long id);

    /**
     * 获取船名和其imo
     * @return
     */
    @Select("SELECT name_of_vessel AS nameOfVessel, imo FROM sp_information WHERE name_of_vessel NOT LIKE '%虚拟船%'")
    List<Map<String, String>> getShipAndImo();

    /**
     * 获取当前查询的列表
     * @return
     */
    @Select("SELECT name_of_vessel AS nameOfVessel, imo , cert_name AS certName, sign_agency AS signAgency FROM sp_qualifications  WHERE del_apply = 0")
    List<Map<String, String>> getNowShipAndImo();

    /**
     * 根据船舶名字查询imo
     * @param imo
     * @return
     */
    @Select("SELECT name_of_vessel FROM sp_information WHERE imo = #{imo}")
    String getNameByImo(String imo);

    /**
     * 绑定修改时添加的文件
     * @param id
     * @param quaId
     */
    @Update("UPDATE sp_fob_file_record SET overhaul_id = #{quaId} WHERE id = #{id}")
    void upDateFileId(@Param("id") String id, @Param("quaId") String quaId);

    @Update("UPDATE sp_qualifications SET expiration_time = NULL,sign_date = NULL WHERE id = #{id}")
    void deleteSignDate(@Param("id") Long id);

    List<SpQualifications> queryQualificationList(@Param("query") SpQualificationsBo query);
}
